<template>
  <div>
    <!-- 面包屑导航 -->
    <Breadcrumbs :breadtitle="{ '1': '用户管理', '2': '用户列表' }"></Breadcrumbs>

    <el-card>
      <!-- 输入添加 -->
      <el-row :gutter="20" type="flex" justify="end">
        <el-col :span="8" :pull="1">
          <div>
            <el-input placeholder="请输入内容" @clear="getUser()" clearable v-model="queryInfo.query">
              <el-button slot="append" icon="el-icon-search" @click="getUser()"></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="2" :pull="1">
          <el-button @click="diaAddUsers = true">添加用户</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="UserList" border style="width: 100%" stripe>
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="username" label="用户名" width="180"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{scope.row.create_time|times('hours')}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="role_name" label="管理权限"> </el-table-column>
        <el-table-column label="操作" width="220">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="Setusers(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="DelUser(scope.row.id)">删除</el-button>
            <el-button size="mini" type="warning" @click="BtnSetUsersRoles(scope.row)">分配</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination :page-sizes="[1, 2, 5, 7]" :page-size="queryInfo.pagesize" :current-page="queryInfo.pagenum" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>

      <AddUsers @OutdiaAdd="diaAddUsers=false" @getUser="getUser" :diaAddUsers="diaAddUsers"></AddUsers>
      <SetUsers @OutdiaSet="diaSetUsers=false" @getUser="getUser" :diaSetUsers="diaSetUsers" :SetUsersData="SetUsersData"></SetUsers>
      <SetUsersRoles @OutdiaSetRoles="diaSetUsersRoles=false" @getUser="getUser" :diaSetUsersRoles="diaSetUsersRoles" :SetUsersRolesData="SetUsersRolesData"></SetUsersRoles>
    </el-card>
  </div>
</template>

<script>
import Breadcrumbs from '@/components/Breadcrumbs'
import AddUsers from '@/components/Users/AddUsers'
import SetUsers from '@/components/Users/SetUsers'
import SetUsersRoles from '@/components/Users/SetUsersRoles'

export default {
  name: 'Users',
  data() {
    return {
      diaAddUsers: false,
      diaSetUsers: false,
      diaSetUsersRoles: false,

      SetUsersRolesData: null,
      UserList: null,
      SetUsersData: {
        id: '',
        username: ''
      },
      queryInfo: {
        query: '',
        pagenum: 1, //当前页数
        pagesize: 7, //每页显示多少条数据
      },
      total: 0,
    }
  },
  components: {
    Breadcrumbs,
    AddUsers,
    SetUsers,
    SetUsersRoles
  },
  created() {
    this.getUser()
    this.$http.get('users/502').then((res) => {
      console.log(res)
    })
  },
  methods: {
    getUser() {
      //获取管理员信息
      this.$http.get('users', { params: this.queryInfo }).then((res) => {
        if (res.data.meta.status !== 200) return
        this.UserList = res.data.data.users
        this.total = res.data.data.total
      })
    },

    handleSizeChange(newSize) {   //监听pagesize改变事件
      this.queryInfo.pagesize = newSize
      this.getUser()
    },
    handleCurrentChange(newNum) {   //监听 页码值 改变事件
      this.queryInfo.pagenum = newNum
      this.getUser()
    },
    DelUser(id) {  //删除账户
      this.$confirm('此操作将永久删除该账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$http.delete(`users/${id}`).then((res) => {
            if (res.data.meta.status !== 200) {
              this.$mess(res.data.meta.msg, 'error')
              return
            }
            this.$mess(res.data.meta.msg, 'success')
            this.getUser()
          })
        })
        .catch(() => {
          this.$mess('已取消删除', 'info')
        })
    },
    Setusers(item) {  //传递id和用户名
      this.SetUsersData.id = item.id
      this.SetUsersData.username = item.username
      this.diaSetUsers = true;
    },
    BtnSetUsersRoles(item) {
      this.diaSetUsersRoles = true
      this.SetUsersRolesData = item
    }
  },
}
</script>

<style lang="less" scoped>
</style>